package com.xiaochi.subject.domain.handler.subject;

import com.xiaochi.subject.common.enums.IsDeletedEnum;
import com.xiaochi.subject.common.enums.SubjectInfoTypeEnum;
import com.xiaochi.subject.domain.convert.MultipleSubjectConverter;
import com.xiaochi.subject.domain.convert.RadioSubjectConverter;
import com.xiaochi.subject.domain.entity.SubjectAnswerBO;
import com.xiaochi.subject.domain.entity.SubjectInfoBO;
import com.xiaochi.subject.domain.entity.SubjectOptionBO;
import com.xiaochi.subject.infra.basic.entity.SubjectMultiple;
import com.xiaochi.subject.infra.basic.entity.SubjectRadio;
import com.xiaochi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 * @author zqx
 * @since 2024/8/13
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.converterBoToEntity(option);
            subjectRadio.setId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedEnum.UN_DELETED_ENUM.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.insertBatch(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOS = RadioSubjectConverter.INSTANCE.converterEntityListToBo(subjectRadioList);
        SubjectOptionBO optionBO = new SubjectOptionBO();
        optionBO.setOptionList(subjectAnswerBOS);
        return optionBO;
    }
}
